package SiTuQingYou.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import SiTuQingYou.util.DBHelper;

public class ScenePhotoDao
{
	private  ScenePhotoDao() {}
	private static  ScenePhotoDao spd= new  ScenePhotoDao();
	public static  ScenePhotoDao getspd()
	{
		return spd;
	}
	public void newphoto(int sceneid,String scenepic) {
		DBHelper dbhelper = DBHelper.getDB();
		  Connection conn = null;
			PreparedStatement ps = null;
			ResultSet rs = null; 
		
		 try {
			 conn = dbhelper.getConn();
			 String sql = "insert into scenephoto(sceneid,scenepic)values(?,?)";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, sceneid);
			ps.setString(2, scenepic);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 finally {
			 dbhelper.close(rs, ps, conn);
		 }
	}
	
	public ArrayList<String> getallphotobyscenesid(int id)
	{
		DBHelper dbhelper = DBHelper.getDB();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null; 
		ArrayList<String> allpics = new ArrayList<String>();
		
		try {
			conn = dbhelper.getConn();
			String sql = "select * from scenephoto where sceneid=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while(rs.next())
			{
				allpics.add(rs.getString("scenepic"));
				
			}
			
			return allpics;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally {
			dbhelper.close(rs, ps, conn);
		}
		return null;
		
	}
	
	public String getfirstphotobyscenesid(int id)
	{
		DBHelper dbhelper = DBHelper.getDB();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null; 
		
		try {
			conn = dbhelper.getConn();
			String sql = "select * from scenephoto where sceneid=? limit 0,1";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			rs.next();
			String pic = rs.getString("scenepic");
			return pic;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally {
			dbhelper.close(rs, ps, conn);
		}
		return null;
		
	}
}